package com.benshou.bsoa.scheduler;

import com.benshou.bsoa.dto.yida.req.YiDaReq;
import com.benshou.bsoa.service.yida.SynchronousProjectAnalysisService;
import com.benshou.bsoa.utils.RetryExecutor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;


/*
 *  同步项目分析调度器
 * 每个月16号的早上9点整点执行
 *
 */
@Component
public class SynchronousProjectAnalysisScheduler {

    private static final Logger log = LoggerFactory.getLogger(SynchronousProjectAnalysisScheduler.class);

    @Autowired
    private SynchronousProjectAnalysisService synchronousProjectAnalysis;

    @Value("${dingding.benshou.userId}")
    private String userId;

    @Value("${dingding.benshou.formUuid}")
    private String formUuid; //项目台账formUuid

    @Autowired
    private RetryExecutor retryExecutor;

    @Scheduled(cron = "0 0 8 16 * ?") // 每月16号早上8点执行一次
    public void scheduledInvoke() throws Exception {
        retryExecutor.runWithRetry(() -> {
            YiDaReq req = new YiDaReq(userId, formUuid);
            try {
                synchronousProjectAnalysis.synchronousProjectAnalysisService(req);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }, 5, 10000, "同步项目分析任务");
    }
}
